﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using Models;
using System.Data;


public partial class TagList : AdminBase
{

    private DataTable dc = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataClassBind(ddlState);
            grdbind();
        }
    }

    public void grdbind()
    {
        using (Entities bll = new Entities())
        {
            var Bpl = from p in bll.S_Goods.OrderBy(p => p.GArea).ThenBy(p => p.GSort) select p;

            var i = ddlState.SelectedValue;

            if (ddlState.SelectedValue != "请选择")
            {
                int _ClassId = int.Parse(ddlState.SelectedValue);
                Bpl = Bpl.Where(p => p.SCCLASSID == _ClassId);
            }

            if (SPName.Text.Trim() != "")
            {
                Bpl = Bpl.Where(p => p.GName.Contains(SPName.Text.Trim()));
            }


            AspNetPager1.RecordCount = Bpl.Count();
            this.rptList.DataSource = Bpl.Skip((AspNetPager1.CurrentPageIndex - 1) * AspNetPager1.PageSize).Take(AspNetPager1.PageSize).ToList();  //这里好像一定要Tolist();不然会有点错误；;
            this.rptList.DataBind();
        }
    }

    protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
    {   //当页面页数改变时，执行这个方法；
        AspNetPager1.CurrentPageIndex = e.NewPageIndex;
        grdbind();
    }
    protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        int id = Convert.ToInt32(((Label)e.Item.FindControl("lb_id")).Text);
        Entities bll = new Entities();
        S_Goods model = bll.S_Goods.First(p => p.GID == id);
        switch (e.CommandName.ToLower())
        {
            case "lock":
                if (model.IsLock == true)
                {
                    model.IsLock = false;
                }
                else
                {
                    model.IsLock = true;
                }
                break;
        }
        bll.SaveChanges();
        grdbind();

    }

    //改变状态点击事件
    public void change_Click(object sender, EventArgs e)
    {
        Entities db = new Entities();
        LinkButton lb = (LinkButton)sender;
        if (lb.CommandName == "lock")
        {
            int _Id = Convert.ToInt32(lb.CommandArgument);
            var bll = db.S_Goods.SingleOrDefault(p => p.GID == _Id);
            if (bll != null)
            {
                if (bll.IsLock == true)
                {
                    bll.IsLock = false;
                }
                else
                {
                    bll.IsLock = true;
                }
                db.SaveChanges();
            }
        }
    }

    //获取状态
    public string getState(bool Sta)
    {
        string result = "";
        if (Sta == true)
        {
            result = "正常";
        }
        else
        {
            result = "<font color=red>锁定</font>";
        }
        return result;
    }

    //通过ID找到兑换券
    public static string GetCertifiNameById(string CodeId)
    {
        int id = int.Parse(CodeId);

        string result = "";
        //Entities db = new Entities();
        //try
        //{
        //    var info = db.S_CodeClass.FirstOrDefault(p => p.CodeCLassID == id);
        //    result = info.CCname;
        //}
        //catch
        //{
        //    result = "为空";
        //}
        return result;
    }

    private void DataClassBind(DropDownList dp)
    {
        Entities db = new Entities();
        var mydb = db.S_Classs;
        dp.DataValueField = "SCID";
        dp.DataTextField = "SCNmae";
        dp.DataSource = mydb.ToList();
        dp.DataBind();
        dp.Items.Insert(0, "请选择");
    }

    protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
    {
        grdbind();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        grdbind();
    }
}

